package xhq.com.test.extspring.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;
import xhq.com.test.extspring.bean.model.MyBeanClass;

/**
 * @author hq_xu
 * @since 2021/1/26 11:37
 */
@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if(beanClass.getName().startsWith("xhq")) {
            if(beanName.equalsIgnoreCase("myBeanClass")) {
                var newInstance = new MyBeanClass();
                newInstance.setGivenName("lala");
                System.out.println("from MyInstantiationAwareBeanPostProcessor");
                System.out.println(newInstance.hashCode());
                return newInstance;
            }
        }
        return null;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean.getClass().getName().startsWith("xhq")){
            System.out.println("from postProcessBeforeInitialization");
        }
        return bean;
    }
}
